<?php
abstract class usuariosDAOImpl {
    public static function Logar($usuario, $senha) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, name, email from users where email=? and password=? and active=1";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ss', $usuario, $senha);
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_id, $data_name,$data_email);
            while (DB::$_stmt->fetch()) {
                $data[] = array('id'=>$data_id,'name'=>$data_name,'email'=>$data_email);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Select($search) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, name, email, password from users";
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_id, $data_name,$data_email);
            while (DB::$_stmt->fetch()) {
                $data[] = array('id'=>$data_id,'name'=>$data_name,'email'=>$data_email);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($active,$admin,$name,$email,$password) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into users (actice,admin,name,email,password) values (?,?,?,?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('iisss', $active,$admin,$name,$email,$password);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }
    public static function Activate($active,$id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update users set active=? where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii', $active,$id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }
    public static function Delete($codigo) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from user where codigo = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $codigo);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

}

?>
